﻿using MobilTomegSzerverWCF.ServiceObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobilTomegSzerverWCF
{
    public partial class RemoteMobile : IRemoteMobile
    {
        public MoveObject[] Move(MoveObject data)
        {
            //hír beillesztése ha van
            /*TODO*/

            foreach (var item2 in routes)
            {
                foreach (var item in item2.Value)
                {
                    if (item.Value.LastUpdate < DateTime.Now.AddSeconds(-15.0))
                    {
                        item.Value.Active = false;
                    }
                }
            }

            Guid a = Guid.NewGuid();
            if (!string.IsNullOrEmpty(data.Guid))
            {
                a = new Guid(data.Guid);
            }

            if (!routes.ContainsKey(data.Jarat))
            {
                routes.Add(data.Jarat, new Dictionary<Guid, RouteData>());
            }

            if (routes[data.Jarat].ContainsKey(a))
            {
                routes[data.Jarat][a] = new RouteData
                {
                    Jarat = data.Jarat,
                    Direction = null,
                    Active = true,
                    Crash = data.Lerobbant,
                    Ellenor = data.Ellenor,
                    Latitude = data.Latitude,
                    Longitude = data.Longitude,
                    LastUpdate = DateTime.Now
                };
            }
            else
            {
                routes[data.Jarat].Add(a, new RouteData
                {
                    Jarat = data.Jarat,
                    Direction = null,
                    Active = true,
                    Crash = data.Lerobbant,
                    Ellenor = data.Ellenor,
                    Latitude = data.Latitude,
                    Longitude = data.Longitude,
                    LastUpdate = DateTime.Now
                });
            }

            List<MoveObject> ret = new List<MoveObject>();

            //alllock.AcquireReaderLock(3000);
            foreach (var item in routes[data.Jarat])
            {
                if (item.Value.Active)
                {
                    ret.Add(new MoveObject
                    {
                        Jarat = data.Jarat,
                        Latitude = item.Value.Latitude,
                        Longitude = item.Value.Longitude,
                        Ellenor = item.Value.Ellenor,
                        Lerobbant = item.Value.Crash,
                        Guid = a.ToString()
                    });
                }
            }

            foreach (var exj in data.ExtraJaratok)
            {
                if (routes.ContainsKey(exj)) foreach (var item in routes[exj])
                    {
                        if (item.Value.Active)
                        {
                            ret.Add(new MoveObject
                            {
                                Jarat = exj,
                                Latitude = item.Value.Latitude,
                                Longitude = item.Value.Longitude,
                                Ellenor = item.Value.Ellenor,
                                Lerobbant = item.Value.Crash,
                                Guid = a.ToString()
                            });
                        }
                    }
            }

            return ret.ToArray();
        }

        public MoveObject[] Wait(string[] route, string news)
        {
            //hír beillesztése ha van
            /*TODO*/

            foreach (var item2 in routes)
            {
                foreach (var item in item2.Value)
                {
                    if (item.Value.LastUpdate < DateTime.Now.AddMinutes(-5.0))
                    {
                        item.Value.Active = false;
                    }
                }
            }

            List<MoveObject> ret = new List<MoveObject>();

            //alllock.AcquireReaderLock(3000);
            foreach (var rou in route)
            {
                if (routes.ContainsKey(rou)) foreach (var item in routes[rou])
                    {
                        if (item.Value.Active)
                        {
                            ret.Add(new MoveObject
                            {
                                Jarat = rou,
                                Latitude = item.Value.Latitude,
                                Longitude = item.Value.Longitude,
                                Ellenor = item.Value.Ellenor,
                                Lerobbant = item.Value.Crash
                            });
                        }
                    }
            }


            return ret.ToArray();
        }

        public string StartTravel()
        {
            return Guid.NewGuid().ToString();
        }

        public void StopTravel(string jarat, string guid)
        {
            Guid g = new Guid(guid);
            routes[jarat].Remove(g);
        }
    }
}
